package embeded.impl;

import java.rmi.RemoteException;

import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;

import operations.Operation;
import operations.OperationType;
import operations.Replicated;
import server.RemoteDomaineManager;
import topic.Topic;
import topic.impl.TopicImpl;
import user_embeded.UserEmbededObject;
import data.DataObject;
import embeded.EmbededObject;

public class EmbededObjectImpl implements EmbededObject {
	private RemoteDomaineManager remoteDomaine = null;
	private UserEmbededObject userEmbededObject = null;
	private Topic topic = null;
	private ORB orb = null;
	private POA poa = null;

	public EmbededObjectImpl(UserEmbededObject userEmbededObject) {
		this.userEmbededObject = userEmbededObject;
	}

	public void doMulticast() throws RemoteException {
		//a definir
	}

	public void setOperation(Operation operation, boolean isRefreshing)
			throws RemoteException {
		topic.getSuscriber().publish(operation);
		notifyRefresh();
	}

	public void notifyRefresh() throws RemoteException {
		userEmbededObject.setRefresh(true);
	}

	public void notifyEnd() throws RemoteException {
		//a definir
		userEmbededObject.endUsing();
	}

	public Object getData() throws RemoteException {
		if (topic == null) {
			topic = new TopicImpl(userEmbededObject.getObjectUserName());
			String ior = "";// a definir
			remoteDomaine.bootstrap(ior, userEmbededObject.getObjectUserName());
			while (!userEmbededObject.isRefresh())
				;
		}
		userEmbededObject.setRefresh(false);
		return topic.getDataObject().getData().extract_Object();
	}

	public void setData(Object data) throws RemoteException {
		// a definir
		DataObject dataObj = new DataObject(orb.create_any()/* .insert_any(data) */);
		Operation op = new Operation(OperationType._update, "cache",
				Replicated.isReplicated, dataObj);
		topic.getSuscriber().publish(op);
		remoteDomaine.addOperation(userEmbededObject.getObjectUserName(), op);
	}

	public void registerRemoteDomaine(String remoteDomaineIor)
			throws RemoteException {

	}

}
